import axios, { AxiosResponse, AxiosRequestConfig } from 'axios';

const baseURL = 'http://10.237.144.179:5000'

export interface User {
  name: string,
  age: number
}

// const user: User = {
//   name: 'aa',
//   age: 11
// }

const user2: User = {
  name: 'bb',
  age: 22
}

axios.interceptors.request.use((config: AxiosRequestConfig): AxiosRequestConfig => {
  if (config?.headers) {
    config.headers.name += '1'
  }
  return config
})
axios.interceptors.request.use((config: AxiosRequestConfig): AxiosRequestConfig => {
  if (config?.headers) {
    config.headers.name += '2'
  }
  return config
})
axios.interceptors.request.use((config: AxiosRequestConfig): AxiosRequestConfig => {
  if (config?.headers) {
    config.headers.name += '3'
  }
  return config
})

axios.interceptors.response.use((response: AxiosResponse): AxiosResponse => {
  response.data.name += '1'
  return response
})
axios.interceptors.response.use((response: AxiosResponse): AxiosResponse => {
  response.data.name += '2'
  return response
})
axios.interceptors.response.use((response: AxiosResponse): AxiosResponse => {
  response.data.name += '3'
  return response
})

// axios({
//   method: 'post',
//   url: baseURL + '/post',
//   data: user2,
//   timeout: 1000,
//   headers: { 'Content-Type': 'application/json', name: 'zf' },
// }).then((response: AxiosResponse<User>) => {
//   console.log(response);
//   return response.data
// }).then((data: User) => {
//   console.log(data);
// }).catch((err: any) => {
//   console.log(err);
// })


// axios<User>({
//   method: 'get',
//   url: baseURL + '/get',
//   params: user
// }).then((response: AxiosResponse<User>) => {
//   console.log(response);
//   return response.data
// }).then((data: User) => {
//   console.log(data);
// }).catch((err: any) => {
//   console.log(err);
// })

axios<User>({
  method: 'post',
  url: baseURL + '/post_timeout?timeout=2000',
  data: user2,
  timeout: 1000,
  headers: { 'Content-Type': 'application/json', name: 'zf' },
}).then((response: AxiosResponse<User>) => {
  console.log(response);
  return response.data
}).then((data: User) => {
  console.log(data);
}).catch((err: any) => {
  console.log(err);
})